﻿using Mall.Core;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace Mall.Web.Entry.Controllers
{
    [Authorize]
    public class UploadController : Controller
    {
        private readonly IWebHostEnvironment _webHostEnvironment;
        public UploadController(IWebHostEnvironment webHostEnvironment)
        {
            _webHostEnvironment = webHostEnvironment;
        }
        #region 单文件上传
        [HttpPost]
        public IActionResult Index(IFormFile file)
        {
            var webRootPath = _webHostEnvironment.WebRootPath;
            var uploadPath = $"/UploadFile/{DateTime.Now:yyyyMMdd}/";//每日文件夹
            try
            {
                //创建每日存储文件夹
                if (!Directory.Exists(Path.Combine(webRootPath, uploadPath)))
                {
                    Directory.CreateDirectory(Path.Combine(webRootPath, uploadPath));
                }
                if (file != null)
                {
                    //文件后缀
                    var fileExtension = Path.GetExtension(file.FileName);//获取文件格式，拓展名
                    var saveName = Path.Combine(uploadPath, "", fileExtension);
                    using var stream = System.IO.File.Create(saveName);
                    file.CopyToAsync(stream);
                    stream.Flush();

                    //完整的文件路径
                    var completeFilePath = Path.Combine(uploadPath, saveName);
                    return Json(new UploadFileResult { Code = "000", Msg = "上传成功", Path = completeFilePath });
                }
                else
                    return Json(new UploadFileResult { Code = "200", Msg = "上传失败，未检测上传的文件信息" });
            }
            catch (Exception ex)
            {

                return Json(new UploadFileResult { Code = "201", Msg = "文件保存失败，异常信息为：" + ex.Message });
            }


        }
        #endregion

        #region 多文件上传
        public IActionResult MultiFileUpload(IFormCollection formCollection)
        {
            var webRootPath = _webHostEnvironment.WebRootPath;
            var uploadFileRequestList = new List<UploadFileRequest>();
            try
            {
                var files = formCollection.Files as FormFileCollection;
                if (files.Any())
                {
                    foreach (var item in files)
                    {
                        var filePath = $"/UploadFile/{DateTime.Now:yyyyMMdd}/";
                        //创建每日存储文件夹
                        if (!Directory.Exists(webRootPath + filePath))
                        {
                            Directory.CreateDirectory(webRootPath + filePath);
                        }
                        //文件后缀
                        var fileExtension = Path.GetExtension(item.FileName);//获取文件格式，拓展名
                        var saveName = Path.Combine(filePath, "", fileExtension);
                        using var stream = System.IO.File.Create(saveName);
                        item.CopyToAsync(stream);
                        stream.Flush();

                        //完整的文件路径
                        var completeFilePath = Path.Combine(filePath, saveName);

                        uploadFileRequestList.Add(new UploadFileRequest()
                        {
                            FileName = saveName,
                            FilePath = completeFilePath
                        });
                    }
                }
                else
                    return Json(new MultiUploadFileResult { Code = "200", Msg = "上传失败，未检测上传的文件信息" });
            }
            catch (Exception ex)
            {

                return Json(new MultiUploadFileResult { Code = "201", Msg = "文件保存失败，异常信息为：" + ex.Message });
            }

            if (uploadFileRequestList.Any())
            {
                return new JsonResult(new MultiUploadFileResult { Code = "000", Msg = "上传成功", Paths = uploadFileRequestList });
            }
            else
            {
                return new JsonResult(new MultiUploadFileResult { Code = "202", Msg = "网络打瞌睡了，文件保存失败" });
            }
        }
        #endregion
    }
}
